<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>
    <!-- jquery -->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css"
          href="/bootstrap/css/bootstrap.min.css"/>
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js">
    </script>
    <!-- layer -->
    <script type="text/javascript" src="/layer/layer.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
        <span id="userTips"> 您还没有登录，请登陆后再操作<br/></span>
        <span>没有收货地址的提示。。。</span>
    </div>
    <table class="table" id="goods">
        <tr>
            <td>商品名称</td>
            <td colspan="3" id="goodsName"></td>
        </tr>
        <tr>
            <td>商品图片</td>
            <td colspan="3"><img id="goodsImg" width="200"
                                 height="200"/></td>
        </tr>
        <tr>
            <td>秒杀开始时间</td>
            <td id="startTime"></td>
            <td>
                <input type="hidden" id="remainSecondsOfStart" name="remainSecondsOfStart">
                <!--                <span if="${seckillStatus eq 0}" id="endOfSeckill">秒杀开始倒计时: <span id="countDown"-->
                <!--                                                                 text="${remainSecondsOfStart}"></span>  秒</span>-->
                <input type="hidden" id="remainSecondsOfEnd" name="remainSecondsOfStart">
                <!--                <span if="${seckillStatus eq 1}" >秒杀结束倒计时: <span id="countDown1"-->
                <!--                                                                   text="${remainSecondsOfEnd}"></span>  秒</span>-->
                <!--                <span if="${seckillStatus eq 2}">秒杀已结束</span>-->
                <span id="seckillTip"></span>
            </td>
            <td>
                <!--                <form action="/seckill/doSeckill" method="post" id="seckillForm" >-->
                <!--                    <input type="hidden" id="goodsId" name="goodsId" >-->
                <!--                    <button class="btn btn-primary btn-block" id="buyButton" type="submit">立即秒杀</button>-->
                <!--                </form>-->

                <button class="btn btn-primary btn-block" id="buyButton" type="button" onclick="doSeckill()">
                    <input type="hidden" id="goodsId" name="goodsId" >
                    立即秒杀
                </button>

            </td>
        </tr>
        <tr>
            <td>商品原价</td>
            <td colspan="3" id="goodsPrice"></td>
        </tr>
        <tr>
            <td>秒杀价</td>
            <td colspan="3" id="seckillPrice"></td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td colspan="3" id="stockCount"></td>
        </tr>
    </table>
</div>
</body>

<script>
    $(function () {
        getDetail();
    });


    function doSeckill(){
        $.ajax({
            url: "/seckill/doSeckill",
            type : "POST",
            data : {
                goodsId : $("#goodsId").val(),
            },
            success : function(data){
                if(data.code==200){
                    window.location.href = "/orderDetail.htm?orderId="+data.object.id;
                }else {
                    layer.msg(data.message)
                }
            },
            error : function() {
                layer.msg("客户端请求错误")
            }


        })
    }

    function doSeckill2(){
        $.ajax({
            url: "/seckill/doSeckill2",
            type : "POST",
            data : {
                goodsId : $("#goodsId").val(),
            },
            success : function(data){
                if(data.code==200){
                    window.location.href = "/orderDetail.htm?orderId="+data.object.id;
                }else {
                    layer.msg(data.message)
                }
            },
            error : function() {
                layer.msg("客户端请求错误")
            }
        })
    }


    function getDetail() {
        var goodsId = g_getQueryString("goodsId");
        $.ajax({
            url: "/goods/detail/" + goodsId,
            type: "GET",
            success: function (data) {
                if (data.code === 200) {
                    render(data.object);
                } else {
                    layer.msg("客户端请求失败1")
                }
            },
            error: function (data) {
                layer.msg("客户端请求失败0")
            }
        });
    }

    function render(detail) {
        var user = detail.user;
        var goods = detail.goodsVo;
        console.log(goods)
        var remainSecondsOfStart = detail.remainSecondsOfStart;
        var remainSecondsOfEnd = detail.remainSecondsOfEnd;
        if (user) {
            $("#userTips").hide();
        }
        $("#goodsName").text(goods.goodsName);
        $("#goodsImg").attr("src", goods.goodsImg);
        $("#startTime").text(new Date(goods.startDate).format("yyyy-MM-dd HH:mm:ss"));
        $("#remainSecondsOfStart").val(remainSecondsOfStart);
        $("#remainSecondsOfEnd").val(remainSecondsOfEnd);
        $("#goodsId").val(goods.id);
        $("#goodsPrice").text(goods.goodsPrice);
        $("#seckillPrice").text(goods.seckillPrice);
        $("#stockCount").text(goods.stockCount);
        countDown();
    }

    function countDown() {
        var remainSecondsOfStart = $("#remainSecondsOfStart").val();
        var remainSecondsOfEnd = $("#remainSecondsOfEnd").val();
        // console.log(remainSecondsOfStart)
        // console.log(remainSecondsOfEnd)
        var timeout;
        var timeout1;
        //秒杀未开始
        if (remainSecondsOfStart > 0) {
            //按钮置灰
            $("#buyButton").attr("disabled", true);
            $("#seckillTip").html('秒杀开始倒计时: ' + remainSecondsOfStart + '秒')

            timeout = setTimeout(function () {
                // $("#countDown").text(remainSecondsOfStart-1);
                $("#remainSecondsOfStart").val(remainSecondsOfStart - 1);
                countDown();
            }, 1000);
            //秒杀进行中
        } else if (remainSecondsOfEnd > 0 && remainSecondsOfStart == 0) {
            // console.log(remainSecondsOfEnd)

            //按钮生效
            $("#buyButton").attr("disabled", false);
            $("#seckillTip").html('秒杀结束倒计时: ' + remainSecondsOfEnd + '秒')
            timeout1 = setTimeout(function () {
                // $("#countDown1").text(remainSecondsOfEnd-1);
                $("#remainSecondsOfEnd").val(remainSecondsOfEnd - 1);
                countDown();
            }, 1000);
            //秒杀结束
        } else {
            //按钮置灰
            $("#buyButton").attr("disabled", true);

            $("#seckillTip").html("秒杀已结束")
        }
    }

</script>
</html>